package Test;

import javax.swing.Timer;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.*;
 class App {
    public static void main(String[] args) {
        // 目标对象
        IUserDao target = new UserDao();
        // 【原始的类型 class com.zhong.UserDao】
        System.out.println(target.getClass());

        // 给目标对象，创建代理对象
        IUserDao proxy = (IUserDao) new ProxyFactory(target).getProxyInstance();
        // class $Proxy0   内存中动态生成的代理对象
        System.out.println(proxy.getClass());

        // 执行方法   【代理对象】
        proxy.save();
        proxy.update();
    }
}
class ProxyFactory{

    //维护一个目标对象
    private Object target;
    public ProxyFactory(Object target){
        this.target=target;
    }

    //给目标对象生成代理对象
    public Object getProxyInstance(){
        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new InvocationHandler() {

                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("开始事务111");
                        System.out.println(method);
                        //执行目标对象方法
                        Object returnValue = method.invoke(target, args);
                        System.out.println("提交事务111");
                        return returnValue;
                    }
                }
        );
    }

}
interface IUserDao {
    void update();
    void save();



}
class UserDao implements IUserDao {
    public void update() {

        System.out.println("----更新数据成功!----");
    }
    public void save() {

        System.out.println("----保存数据成功!----");
    }


}

class LambdaTest{
    public static void main(String[] args) throws InterruptedException {
        String[] planets =new String[]{"Mercury","Venus","Earth","Mars","Jupiter","Saturn"};
        System.out.println(Arrays.toString(planets));
        Arrays.sort(planets);
        System.out.println(Arrays.toString(planets));
        Arrays.sort(planets,(first,sencond)->first.length()-sencond.length());
        System.out.println(Arrays.toString(planets));

//        Timer t=new Timer(1000,event->System.out.println("the time is "+new Date()));
        Timer t=new Timer(1000,System.out::println);
        t.start();
        JOptionPane.showMessageDialog(null,"quit program");
        System.exit(0);
    }
}

class cloneMethod{
    public static void main(String[] args) throws CloneNotSupportedException {
        Employee0 employee0 = new Employee0("huang",1000);

        Employee0 employee01=  employee0.clone();
        employee01.raiseSalary(10);
        System.out.println(employee0.getSalary());
    }
}

class TimerTest{
    public static void main(String[] args) {
        ActionListener listener = new TimePrinter();
        Timer t= new Timer(1000,listener);
        t.start();
        JOptionPane.showMessageDialog(null,"quit program?");
        System.exit(0);
    }
}
class TimePrinter implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("At the time is "+ new Date());
        Toolkit.getDefaultToolkit().beep();
    }
}

class 接口lambda表达式与内部类 {
    public static void main(String[] args) {
//        Employee0[] staff =new Employee0[3];
//        staff[0] = new Employee0("Harry Hacker",35000);
//        staff[1] = new Employee0("Carl Cracker",75000);
//        staff[2] = new Employee0("Tony Tester",38000);
//
//        Arrays.sort(staff);
//        for (Employee0 e:staff){
//            System.out.println("name = "+e.getName()+",salary = "+e.getSalary());
//        }
        List<Employee0> s= new ArrayList<>();
        boolean a=s.isEmpty();
        System.out.println(a);
    }
}
 class Employee0 implements Comparable , Comparator ,Cloneable{
    private String name;
    private double salary;

    public Employee0(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    void raiseSalary(double byPercent){
        double raise = salary * byPercent/100;
        salary+=raise;
    }



     @Override
     public int compareTo(Object o) {
        Employee0 employee0=(Employee0)o;
         System.out.println(employee0.salary);
         System.out.println(salary);
         return Double.compare(salary,employee0.salary);
     }

     @Override
     public int compare(Object o1, Object o2) {
            String a=(String)o1;
         String b=(String)o2;
         return a.length()-b.length();
     }

     public Employee0 clone() throws CloneNotSupportedException {
        return (Employee0) super.clone();
     }
 }
